package com.auxgroup.hr.front.site.co.hireperson;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import com.auxgroup.hr.front.site.vo.person.HireFileRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.Constant;
import com.auxgroup.hr.front.commons.config.BusinessConfig;
import com.auxgroup.hr.front.commons.utils.FtpFileUtil;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.PsCHireFileTbl;
import com.auxgroup.hr.front.dao.domain.PsChireFilePrimary;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.hireperson.HireFileService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.vo.person.HireFileRespVO;

/**
 * 入职人员文件内容CO
 * @author lvliuhao
 * @version 创建时间：180523 
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/hireFile")
public class HireFileCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(HireFileCO.class);

	@Autowired
	public HireFileService hireFileService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;
	
	@Autowired
	private FtpFileUtil ftpUtil;
	
	@Autowired
	private BusinessConfig businessConfig;

	/**
	 * 获取入职人员文件内容
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(String rsHireId,BigDecimal cSeqno,String fileNum) {
		logger.info("getHireFile param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"文件序列不能为空");
		}
		if(StringUtils.isBlank(fileNum)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"fileNum不能为空");
		}
		PsChireFilePrimary filePrimary = new PsChireFilePrimary(rsHireId, cSeqno,fileNum);
		PsCHireFileTbl hireFile = hireFileService.getHireFile(filePrimary);
		return buildRespone(hireFile);
	}
	
	/**
	 * 保存入职人员文件内容
	 * @param hireId
	 * @return
	 */
	@PostMapping(value = "/upload")
	public BaseResponseVO upload(@RequestParam("fileName") String base64, PsCHireFileTbl hireFile) {
		logger.info("saveHireFile param= {}", hireFile);
		if(StringUtils.isBlank(hireFile.getRsHireId())){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		hireFile = hireFileService.save(hireFile.getRsHireId(),base64, hireFile);
		if(hireFile.getSeqno()!=null){
			HireFileRespVO respVO = new HireFileRespVO();
			buildimgUrl(respVO, hireFile);
			return buildRespone(respVO);
		}
		return buildRespone(false);
	}
	
	/**
	 * 删除入职人员文件内容
	 * @param hireId
	 * @return
	 */
	@PostMapping(value = "/del")
	public BaseResponseVO del(String rsHireId,BigDecimal cSeqno,String fileNum) {
		logger.info("delHireFile param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(cSeqno==null){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"文件序列不能为空");
		}
		if(StringUtils.isBlank(fileNum)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"fileNum不能为空");
		}
		PsChireFilePrimary filePrimary =  new PsChireFilePrimary(rsHireId, cSeqno,fileNum);
		Boolean isdel = hireFileService.delete(filePrimary);
		return buildRespone(isdel);
	}
	
	/**
	 * 获取全部
	 * @param rsHireId
	 * @return
	 */
	@GetMapping(value = "/getAll")
	public BaseResponseVO getAll(String rsHireId,String fileNum){
		logger.info("getHireFile param= {}", rsHireId);
		if(StringUtils.isBlank(rsHireId)){
			return buildRespone(BaseRetCode.CODE_ERROR_RSHIREID_BLANK);
		}
		if(StringUtils.isBlank(fileNum)){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL.getRetCode(),"fileNum不能为空");
		}
		List<PsCHireFileTbl> files = hireFileService.getAll(rsHireId,fileNum);
		List<HireFileRespVO> respVOs = new ArrayList<HireFileRespVO>();
		HireFileRespVO respVo;
		if(files!=null&&!files.isEmpty()){
			for (PsCHireFileTbl file : files) {
				respVo = new HireFileRespVO();
				buildimgUrl(respVo,file);
				respVOs.add(respVo);
			}
		}
		return buildRespone(respVOs,false);
	}
	
	
	/**
	 * 构建返回值
	 * @param respVo
	 * @param file
	 */
	private void buildimgUrl(HireFileRespVO respVo, PsCHireFileTbl file){
		objectMapper.map(file, respVo);
		String fileType = file.getAttachsysfilename().substring(file.getAttachsysfilename().lastIndexOf(".")+1, file.getAttachsysfilename().length());
		respVo.setFileType(fileType);
		if(Constant.ALLOWED_DISPLAY_IMG_FORMAT.contains(fileType)){
			StringBuilder jpgPath = new StringBuilder(businessConfig.getFtpFileResourceLocation());
			StringBuilder jpgUrl = new StringBuilder(Constant.FTPFILE_STATIC_PATH);
			if(ftpUtil.downloadFile(hireFileService.getFtpPath(file), file.getAttachsysfilename(), jpgPath.toString())){
				respVo.setImgUrl(jpgUrl.append(file.getAttachsysfilename()).toString());
			}
		}
	}
}
